<?php

namespace App\Admin\RowActions;

use App\Models\Message;
use Dcat\Admin\Grid\RowAction;
use Illuminate\Http\Request;

class Fail extends RowAction
{
    protected $model;

    public function __construct(string $model = null)
    {
        $this->model = $model;
    }

    /**
     * 标题
     *
     * @return string
     */
    public function title()
    {
        return empty($this->row->status) ? '<button type="button" style="background-color: #FF5722" class="layui-btn">不通过</button>' : null;
    }

    /**
     * 设置确认弹窗信息，如果返回空值，则不会弹出弹窗
     *
     * 允许返回字符串或数组类型
     *
     * @return array|string|void
     */
    public function confirm()
    {
        return [
            "审核？",
            '是否审核不通过',
        ];
    }

    /**
     * 处理请求
     *
     * @param Request $request
     *
     * @return \Dcat\Admin\Actions\Response
     */
    public function handle(Request $request)
    {
        $id = $this->getKey();

        $status = $request->get('status');
        $model = $request->get('model');
        $member_id = $request->get('member_id');

        $model::where(['id' => $id, 'status' => $status])->update(['status' => 2]);

        Message::insert([
            'title' => '审核通知',
            'type' => 2,
            'admin_id' => 0,
            'member_id' => $member_id,
            'content' => '您的帖子审核不通过',
            'created_at' => date('Y-m-d H:i:s')
        ]);

        return $this->response()->success("操作成功")->refresh();
    }

    /**
     * 设置要POST到接口的数据
     *
     * @return array
     */
    public function parameters()
    {
        return [
            'status' => $this->row->status,
            'model' => $this->model,
            'member_id' => $this->row->member_id
        ];
    }
}
